﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIIMWeb.YIBAO;
using System.Xml;
using MediSIIM.BizComponent.ZheJiangSYKT;
using SWSoft.Framework;
using System.IO;
using Tools.Unity;
using System.Threading;
using System.Diagnostics;

namespace ZheJiangSYKT
{
    public partial class FormZheJiangSYKT : Form
    {
        public static YB_BXLBXX BXLBXX = Loader.GetLBXX("103");
        TradeInfo info = new TradeInfo();
        public FormZheJiangSYKT()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }


        FileInfo configini = new FileInfo(Application.StartupPath + "\\ZheJiangSYKT.ini");
        private void Form1_Load(object sender, EventArgs e)
        {

            Environment.SetEnvironmentVariable("path", Environment.CurrentDirectory + ";" + Environment.GetEnvironmentVariable("path"));

            Text = string.Format("{0}.{1}.{2} 医疗机构编码:{3}", BXLBXX.YLBXID, BXLBXX.YLBXMC, BXLBXX.YBYYMC, BXLBXX.YBYYDM);
            if (configini.Exists)
            {
                using (var sr = new StreamReader(configini.FullName, Encoding.UTF8))
                {
                    textBox1.Text = sr.ReadLine();
                }
            }
            comboBox1.SelectedIndex = 0;
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            using (var sr = new StreamWriter(configini.FullName, false, Encoding.UTF8))
            {
                sr.WriteLine(textBox1.Text);
            }
            string tradeid = comboBox1.SelectedItem.ToString().Split('.')[0];
            info.TradeCheck(listView1, tradeid);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count < 1)
            {
                MessageBox.Show("请选择要确认的结算记录");
                return;
            }
            Commit("0");
        }

        public void Commit(string result)
        {
            var datarow = listView1.SelectedItems[0].Tag as Entry;
            var sInData = "$$" +
                "1" + "~" +//是否有社会保障卡
                datarow["YBDKXX"] + "~" +//社会保障卡信息
                "" + "~" +//预留1
                "" + "~" +//预留2
                datarow["YBJYHM"] + "~" +//结算流水号
                result + "~" +//就医地提交确认结果
                (comboBox1.SelectedIndex < 2 ? "0" : "1") + "~" +//交易类型 0 门诊 1 住院
                "" + "" +//附加信息
                "$$";
            var callparam = new ZheJiangSYKTTradeParam(9206, sInData);
            info.YBCall(callparam);
            MessageBox.Show(callparam.OutData);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count < 1)
            {
                MessageBox.Show("请选择要确认的结算记录");
                return;
            }
            Commit("-1");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            foreach (var item in DBVisitor.ExecuteModels(textBox1.Text))
            {
                var viewitem = new ListViewItem((item["YBJYHM"] ?? "").ToString());//交易流水号
                viewitem.Tag = item;
                viewitem.SubItems.Add(item["BDJYRQ"].ToString());//日期
                viewitem.SubItems.Add("未查询");//交易状态
                viewitem.SubItems.Add("");//查询交易结果
                viewitem.SubItems.Add((item["YBJSXX"] ?? "").ToString());//对应交易出参
                listView1.Items.Add(viewitem);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var strs = lblcaozuogh.Text.Split('.');
            if (strs.Length != 2)
            {
                MessageBox.Show("请输入业务周期号");
            }
            var sindata = "$$" +
"0" + "~" +//是否有社会保障卡
"" + "~" +//社会保障卡信息
"" + "~" +//预留1
"" + "~" +//预留2
"0" + "~" +//已下载最大平台流水号
strs[1] + "~" +//操作员账号
txtywzqh.Text + "" +//对帐流水号
"$$";
            var callparam = new ZheJiangSYKTTradeParam(9255, sindata);
            info.YBCall(callparam);
            //callparam.OutData = "$$0~~0~6~20121208020170980817334730|20000000000159719919|330683|MZ2_79444|13|F13695733|黄伯忠|2012-12-08|35.05|0|0|1.75|0|33.3|1.75|16.65|0|16.65|0|0|0|0|0|0|0|0|0|0^20121208020170981044464960|551288839|330521|MZ2_79619|13|D10127390|田莹群|2012-12-08|913.3|24|35.56|0|0|725.68|187.62|0|0|0|0|725.68|0|0|0|0|0|24|35.56|128.06^20121208510115541315138730|20000000000159780770|330683|MZ2_79653|13|F13695733|黄伯忠|2012-12-08|604.8|0|18.14|29.33|0|557.33|47.47|278.67|0|278.66|0|0|0|0|0|0|0|0|0|0^20121208510220410858503960|20000000000159731344|330683|MZ2_79474|13|F13694087|黄秋凤|2012-12-08|81.16|10.16|6|3.25|0|61.75|19.41|30.88|0|30.87|0|0|0|0|0|0|0|0|0|0^20121208510220421024078320|20000000000159759975|330683|MZ2_79583|13|F13695733|黄伯忠|2012-12-08|463.35|400|0|3.17|0|60.18|403.17|30.09|0|30.09|0|0|0|0|0|0|0|0|0|0^20121208510220421034035120|551288376|330521|GH2_75793|12|D10127390|田莹群|2012-12-08|3|1|0|0|0|1.7|1.3|0|0|0|0|1.7|0|0|0|0|0|1|0|.3^$$".Trim('$');
            var table = new DataTable();
            table.Columns.Add("平台流水号");
            table.Columns.Add("参保地流水号");
            table.Columns.Add("参保地代码");
            table.Columns.Add("业务序列号");
            table.Columns.Add("医疗类型");
            table.Columns.Add("社会保障卡卡号");
            table.Columns.Add("姓名");
            table.Columns.Add("交易时间");
            table.Columns.Add("医疗费用总额");
            table.Columns.Add("自费总额（非医保）");
            table.Columns.Add("自理总额（目录内自负比例部分）");
            table.Columns.Add("转院自负");
            table.Columns.Add("起付标准");
            table.Columns.Add("报销金额");
            table.Columns.Add("个人现金支付");
            table.Columns.Add("统筹基金支付");
            table.Columns.Add("往年帐户支付");
            table.Columns.Add("当年帐户支付");
            table.Columns.Add("大病救助支付");
            table.Columns.Add("公务员补助支付");
            table.Columns.Add("二乙基金支付");
            table.Columns.Add("离休基金支付");
            table.Columns.Add("劳模基金支付");
            table.Columns.Add("补助基金支付");
            table.Columns.Add("预留1");
            table.Columns.Add("预留2");
            table.Columns.Add("预留3");
            table.Columns.Add("预留4");
            foreach (var item in callparam.OutData.Split('~')[4].Split('^'))
            {
                var newrow = table.NewRow();
                newrow.ItemArray = item.Split('|');
                table.Rows.Add(newrow);
            }
            dataGridView1.DataSource = table;
            dataGridView1.Tag = table;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView2.DataSource = DBVisitor.ExecuteTable(string.Format("SELECT czyhgh as 操作工号,czyhxm as 姓名,yhqdsj as 签到时间,ywzqhm as 业务周期号 FROM yb_ywzcxx where ylbxid={0}{1}", BXLBXX.YLBXID, string.IsNullOrEmpty(textBox3.Text) ? "" : " and czyhgh='" + textBox3.Text + "'"));
        }

        private void txtywzqh_TextChanged(object sender, EventArgs e)
        {
            var list = DBVisitor.ExecuteTable(string.Format("select l.bxysdm czyhgh,r.czyhxm from yb_ywzcxx r left join yb_ysdmdz l on czyhgh=yyysdm  where r.ylbxid=103 and ywzqhm='{0}' and l.ylbxid=101", txtywzqh.Text));
            if (list.Rows.Count > 0)
            {
                lblcaozuogh.Text = list.Rows[0]["czyhxm"] + "." + list.Rows[0]["czyhgh"];
            }
            else
            {
                lblcaozuogh.Text = "";
            }
        }

        private void 取消结算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var strs = lblcaozuogh.Text.Split('.');
            var obj = dataGridView1.SelectedCells[0].OwningRow.Cells;
            string sindata = "";
            var kaxinxi = (DBVisitor.ExecuteScalar("SELECT ybdkxx FROM yb_mzjsxx WHERE ylbxid=103 AND ybbrkh='" + obj["社会保障卡卡号"].Value + "'") ?? "").ToString();
            if (kaxinxi == "")
            {
                MessageBox.Show("没有找到结算记录");
            }
            if (int.Parse(obj["医疗类型"].Value.ToString()) < 21)
            {
                sindata = "$$1~" +// 是否有社会保障卡
                kaxinxi + "~" +//社会保障卡信息
                "~" +//预留1
                "~" +//预留2
                obj["社会保障卡卡号"].Value + "~" +//社会保障卡卡号
                kaxinxi.Split('|')[0] + "~" +//社会保障卡卡的识别码
                obj["参保地流水号"].Value + "~" +//要退费的结算流水号
                strs[1] + "~" +//操作员编号
                strs[0] + "~" +//操作员姓名
                txtywzqh.Text + "~" +//对账流水号
                kaxinxi.Split('|')[10] + "$$";//终端机编号
                var callparam = new ZheJiangSYKTTradeParam(9204, sindata);
                info.YBCall(callparam);
                MessageBox.Show(callparam.OutData);
            }

        }

        private void txtgenxlsh_Click(object sender, EventArgs e)
        {
            var table = dataGridView1.Tag as DataTable;
            foreach (DataRow item in table.Rows)
            {
                try
                {

                    if (int.Parse(item["医疗类型"].ToString()) < 21)
                    {
                        DBVisitor.ExecuteNonQuery(string.Format("update yb_mzjsxx set ybjyhm='{0}' where ylbxid=103 and hismzh='{1}'", item["参保地流水号"], item["业务序列号"]));
                    }
                    else
                    {
                        DBVisitor.ExecuteNonQuery(string.Format("update yb_zyjsxx set ybjyhm='{0}' where ylbxid=103 and ybdjhm='{1}'", item["参保地流水号"], item["业务序列号"]));
                    }
                }
                catch (Exception)
                {
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var sInData = "$$" +
"0" + "~" +//是否有社会保障卡
"" + "~" +//社会保障卡信息
"" + "~" +//预留1
"" + "~" +//预留2
"0" + "~" +//已下载最大平台流水号
dateTimePicker1.Text + "$$";//交易日期         
            var callparam = new ZheJiangSYKTTradeParam(9224, sInData);
            info.YBCall(callparam);
            var table = new DataTable();
            table.Columns.Add("平台流水号");
            table.Columns.Add("参保地流水号");
            table.Columns.Add("参保地代码");
            table.Columns.Add("业务序列号");
            table.Columns.Add("医疗类型");
            table.Columns.Add("社会保障卡卡号");
            table.Columns.Add("姓名");
            table.Columns.Add("交易时间");
            table.Columns.Add("医疗费用总额");
            table.Columns.Add("自费总额（非医保）");
            table.Columns.Add("自理总额（目录内自负比例部分）");
            table.Columns.Add("转院自负");
            table.Columns.Add("起付标准");
            table.Columns.Add("报销金额");
            table.Columns.Add("个人现金支付");
            table.Columns.Add("统筹基金支付");
            table.Columns.Add("往年帐户支付");
            table.Columns.Add("当年帐户支付");
            table.Columns.Add("大病救助支付");
            table.Columns.Add("公务员补助支付");
            table.Columns.Add("二乙基金支付");
            table.Columns.Add("离休基金支付");
            table.Columns.Add("劳模基金支付");
            table.Columns.Add("补助基金支付");
            table.Columns.Add("预留1");
            table.Columns.Add("预留2");
            table.Columns.Add("预留3");
            table.Columns.Add("预留4");
            foreach (var item in callparam.OutData.Split('~')[4].Split('^'))
            {
                var newrow = table.NewRow();
                newrow.ItemArray = item.Split('|');
                table.Rows.Add(newrow);
            }
            dgvjiaoyimx.DataSource = table;
            dgvjiaoyimx.Tag = table;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DBVisitor.GetConnection("ConnectionString1");
            var jbxx = DBVisitor.ExecuteModels(SqlLoad.GetFormat("T00002", "T1129114", "0"));
        }

    }


    public class YB_BRJBXXK : Entry
    {
        public string Jzkh { get; set; }
        public decimal Brlb { get; set; }
        public string Brxzdm { get; set; }
        public string Ybkh { get; set; }
        public string Zh { get; set; }
        public string Bah { get; set; }
        public string Xm { get; set; }
        public string Xb { get; set; }
        public string Mz { get; set; }
        public string Csrq { get; set; }
        public string ZHENGJIANLX { get; set; }
        public string Sfzh { get; set; }
        public string DANWEILX { get; set; }
        public string Dwbh { get; set; }
        public string Dwmc { get; set; }
        public string Jtzz { get; set; }
        public string Ybryxz { get; set; }
    }
}
